<?php
/**
 * 
 * User/IndexAction.class.php (前台会员中心模块)
 *
 * @package      	jgsat
 * @author          liuxun QQ:147613338 <admin@jgsat.cn>
 * @copyright     	Copyright (c) 2008-2011  (http://www.jgsat.com)
 * @license         http://www.jgsat.com/license.txt
 * @version        	jgsat企业网站管理系统 v2.1 2012-10-08 jgsat.cn $
 */
if(!defined("JGsat")) exit("Access Denied");
class OauthAction extends BaseAction
{

    function _initialize()
    {	
	parent::_initialize();
	
		$this->sina = array(
			"App_Key" => '3629123384',
			"App_Secret" =>'c52c1fc919629661b362ee23934be1de',
			"WB_CALLBACK_URL" => 'http://www.99cykj.com'
			);

    }

    public function qqlogin()
    {
        //$jgqq=M("Oauth")->where("title='qq'")->find();
        import ( "@.Oauth.QQ.qqConnectAPI" );
        $qc = new QC();
        $qc->qq_login();
}
	
	
    public function qqcallback()
    {

        //$jgqq=M("Oauth")->where("title='qq'")->find();
        import ( "@.Oauth.QQ.qqConnectAPI" );

        $qc = new QC();

        $qqcallbackstr=$qc->qq_callback();

        $qqopenid=$qc->get_openid();

        $this->assign('openid',$qqopenid);

        $qc = new QC($qqcallbackstr,$qqopenid);
        $qquserinfo = $qc->get_user_info();
        $this->assign('qquserinfo',$qquserinfo);
      //  print_r( $qquserinfo);exit;
        $this->assign('qquser',$qquserinfo);
        $qquser=M("User")->where("openid='".$qqopenid."'")->find();

        if($qquser){
            $cookietime =  0;

            
            $jgsat_auth_key = sysmd5($this->sysConfig['ADMIN_ACCESS'].$_SERVER['HTTP_USER_AGENT']);
            $jgsat_auth = authcode($qquser['id']."-".$qquser['groupid']."-".$qquser['password'], 'ENCODE', $jgsat_auth_key);

            cookie('auth',$jgsat_auth,$cookietime);
            cookie('username',$qquser['username'],$cookietime);
            cookie('groupid',$qquser['groupid'],$cookietime);
            cookie('userid',$qquser['id'],$cookietime);
            cookie('email',$qquser['email'],$cookietime);
            cookie('createtime',$qquser['createtime'],$cookietime);
            //保存登录信息
            $dao = M('User');
            $data = array();
            $data['id']	=$qquser['id'];
            $data['last_logintime']=time();
            $data['last_ip']= get_client_ip();
            $data['login_count']=array('exp','login_count+1');
            $dao->save($data);
          //  $this->assign('closeWin',1);
            $forward = $_POST['forward'] ? $_POST['forward'] :$this->forward ;

            $forward = URL('User-Index/index');
            $this->assign('jumpUrl',$forward);
            $this->success(L('login_ok'));

        }
        else{
            $this->display("Oauth:register");

        }
    }
    
    
    public function sina_login()
    {
        
        import ( "@.Oauth.sina.saetv2" );
		$included_files = get_included_files();
        //print_r($included_files);exit;
        $obj = new SaeTOAuthV2($this->sina['App_Key'],$this->sina['App_Secret']);
        $sina_url = $obj->getAuthorizeURL( $this->sina['WB_CALLBACK_URL'] );
        redirect($sina_url);
        //return $sina_url;
    }
	
	 public function sina_callback(){
		  import ( "@.Oauth.sina.saetv2" );
		  $obj = new SaeTOAuthV2($this->sina['App_Key'],$this->sina['App_Secret']);
          $code=$_REQUEST['code'];
		  if (isset($code)) {
		  $keys = array();
		  $keys['code'] = $code;
		  $keys['redirect_uri'] = $this->sina['WB_CALLBACK_URL'];
		  try {
			$token = $obj->getAccessToken( 'code', $keys ) ;//完成授权
		  } catch (OAuthException $e) {
		}
		  }
          //print_r($token);exit;
		  $c = new SaeTClientV2($this->sina['App_Key'], $this->sina['App_Secret'], $token['access_token']);
		  $ms =$c->home_timeline();
		  $uid_get = $c->get_uid();//获取u_id
		  $uid = $uid_get['uid'];
          $this->assign('sina_uid',$uid);
          $sinauser=M("User")->where("sina_uid='".$uid."'")->find();

         // $user_message = $c->show_user_by_id($uid);//获取用户信息
        // print_r($user_message);exit;
		  //return $user_message;
             if($sinauser){
                 $cookietime =  0;
                 $jgsat_auth_key = sysmd5($this->sysConfig['ADMIN_ACCESS'].$_SERVER['HTTP_USER_AGENT']);
                 $jgsat_auth = authcode($sinauser['id']."-".$sinauser['groupid']."-".$sinauser['password'], 'ENCODE', $jgsat_auth_key);

                 cookie('auth',$jgsat_auth,$cookietime);
                 cookie('username',$sinauser['username'],$cookietime);
                 cookie('groupid',$sinauser['groupid'],$cookietime);
                 cookie('userid',$sinauser['id'],$cookietime);
                 cookie('email',$sinauser['email'],$cookietime);
                 cookie('createtime',$sinauser['createtime'],$cookietime);
                 //保存登录信息
                 $dao = M('User');
                 $data = array();
                 $data['id']	=$sinauser['id'];
                 $data['last_logintime']=time();
                 $data['last_ip']= get_client_ip();
                 $data['login_count']=array('exp','login_count+1');
                 $dao->save($data);
                 //  $this->assign('closeWin',1);
                 $forward = $_POST['forward'] ? $_POST['forward'] :$this->forward ;

                 $forward = URL('User-Index/index');

                 $this->assign('jumpUrl',$forward);
                 $this->success(L('login_ok'));

             }
             else{
                 $this->display("Oauth:register");

             }
		
		}
   
}
?>